package com.cqvie.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cqvie.model.AttendDetails;

@Repository(value = "attendDeatilsDao")
public class AttendDetailsDao {
	@Resource
	private SessionFactory sessionFactory;

	// 根据考勤的对象来修改相应的考勤记录
	public boolean modifyByName(String mark,int stuID) {
		int temp;
		String hql = "update AttendDetails at set at.a_mark=?  where at.stuID=?";
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery(hql);
		q.setString(0, mark);
		q.setInteger(1, stuID);
		temp = q.executeUpdate();
		if (temp >= 1) {
			return true;
		} else
			return false;
	}

	// 根据学生姓名来查询考勤记录
	@SuppressWarnings("unchecked")
	public List<AttendDetails> selectBystuName(String stuNam) {
		String hql = "from AttendDetails attendD where attendD.stuName=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setString(0, stuNam);
		List<AttendDetails> atD = query.list();
		if (atD.size() >= 1) {
			return atD;
		}else
			return null;
	}
	
	//通过考勤ID来查询考勤明细
	@SuppressWarnings("unchecked")
	public List<AttendDetails> selectByattendID(String id){
		String hql = "from AttendDetails attendD where attendD.attend.ID=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setString(0, id);
		List<AttendDetails> atD = query.list();
		if (atD.size() >= 1) {
			return atD;
		}
		return null;
	}

	//添加考勤明细
	public void addAttendDeatils(AttendDetails ad){
		Session session=sessionFactory.getCurrentSession();
		session.save(ad);
	}
	
}
